package com.ruoyi.erp.controller;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.erp.domain.ProjectBonus;
import com.ruoyi.erp.service.IProjectBonusService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目提成Controller
 * 
 * @author sauger
 * @date 2022-04-21
 */
@Controller
@RequestMapping("/erp/ProjectBonus")
public class ProjectBonusController extends BaseController
{
    private String prefix = "erp/ProjectBonus";

    @Autowired
    private IProjectBonusService projectBonusService;

    @RequiresPermissions("erp:ProjectBonus:view")
    @GetMapping()
    public String ProjectBonus(ModelMap mmap)
    {
        Date beginTime = DateUtils.setDays(new Date(), 1);
        Date endTime = DateUtils.addMonths(beginTime, 1);
        endTime = DateUtils.addDays(endTime, -1);
        mmap.put("beginTime", DateUtils.parseDateToStr("yyyy-MM-dd", beginTime));
        mmap.put("endTime", DateUtils.parseDateToStr("yyyy-MM-dd", endTime));
        return prefix + "/ProjectBonus";
    }

    /**
     * 查询项目提成列表
     */
    @RequiresPermissions("erp:ProjectBonus:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProjectBonus projectBonus)
    {
        startPage();
        List<ProjectBonus> list = projectBonusService.selectProjectBonusList(projectBonus);
        return getDataTable(list);
    }

    /**
     * 导出项目提成列表
     */
    @RequiresPermissions("erp:ProjectBonus:export")
    @Log(title = "项目提成", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProjectBonus projectBonus)
    {
        List<ProjectBonus> list = projectBonusService.selectProjectBonusList(projectBonus);
        ExcelUtil<ProjectBonus> util = new ExcelUtil<ProjectBonus>(ProjectBonus.class);
        return util.exportExcel(list, "项目提成数据");
    }

    /**
     * 新增项目提成
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    @GetMapping("/calculate")
    public String calculateView(ModelMap mmap){
        //当月第一天
        Date firstDate = DateUtils.setDays(new Date(), 1);
        Date lastDate = DateUtils.addMonths(firstDate, 1);
        lastDate = DateUtils.addDays(lastDate, -1);
        mmap.put("firstDate", DateUtils.parseDateToStr("yyyy-MM-dd", firstDate));
        mmap.put("lastDate", DateUtils.parseDateToStr("yyyy-MM-dd", lastDate));
        return prefix + "/calculate";
    }

    @PostMapping("/calculate")
    @ResponseBody
    public AjaxResult calculate(ProjectBonus projectBonus){
        Date beginDate = DateUtils.parseDate(projectBonus.getParams().get("beginDate"));
        Date endDate = DateUtils.parseDate(projectBonus.getParams().get("endDate"));
        projectBonusService.updateBonusByDuration(beginDate, endDate);
        return toAjax(true);
    }

    /**
     * 新增保存项目提成
     */
    @RequiresPermissions("erp:ProjectBonus:add")
    @Log(title = "项目提成", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ProjectBonus projectBonus)
    {
        return toAjax(projectBonusService.insertProjectBonus(projectBonus));
    }

    /**
     * 修改项目提成
     */
    @RequiresPermissions("erp:ProjectBonus:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        ProjectBonus projectBonus = projectBonusService.selectProjectBonusById(id);
        mmap.put("projectBonus", projectBonus);
        return prefix + "/edit";
    }

    /**
     * 修改保存项目提成
     */
    @RequiresPermissions("erp:ProjectBonus:edit")
    @Log(title = "项目提成", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProjectBonus projectBonus)
    {
        return toAjax(projectBonusService.updateProjectBonus(projectBonus));
    }

    /**
     * 删除项目提成
     */
    @RequiresPermissions("erp:ProjectBonus:remove")
    @Log(title = "项目提成", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(projectBonusService.deleteProjectBonusByIds(ids));
    }

    @RequiresPermissions("erp:ProjectBonus:report")
    @GetMapping("/report")
    public String projectBonusReport(ModelMap mmap)
    {
        Date beginTime = DateUtils.setDays(new Date(), 1);
        Date endTime = DateUtils.addMonths(beginTime, 1);
        endTime = DateUtils.addDays(endTime, -1);
        mmap.put("beginTime", DateUtils.parseDateToStr("yyyy-MM-dd", beginTime));
        mmap.put("endTime", DateUtils.parseDateToStr("yyyy-MM-dd", endTime));
        return prefix + "/report";
    }

    @RequiresPermissions("erp:ProjectBonus:report")
    @PostMapping("/report")
    @ResponseBody
    public TableDataInfo reportList(ProjectBonus projectBonus)
    {
        startPage();
        List<ProjectBonus> list = projectBonusService.groupProjectBonusByUser(projectBonus);
        return getDataTable(list);
    }
}
